//package cn.jiyun.gateway.filter;
//
//
//
//
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.JSONObject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
//@Component
//public class AuthGlobalFilter implements GlobalFilter {
//
//    @Autowired
//    private RedisTemplate<String,String> redisTemplate;
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        /*
//         * 1.全局过滤器对所有的请求进行拦截
//         * 2.找到请求路径中带有“auth”字样的
//         *   查看是否有token，并且token是否有效
//         * 3.如果token有效或者路径中不带auth，都放行
//         *
//         * 4.路径中有auth但是token没有或者token无效，返回错误信息
//         * */
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//        String path = request.getURI().getPath();
//        //路径匹配
//        AntPathMatcher antPathMatcher = new AntPathMatcher();
//        User userInfo = getUserInfo(request);
//        if (antPathMatcher.match("/auth/**",path)){
//            System.out.println("这个路径需要鉴权  ，该请路径是=====>>" + path);
//            if (userInfo == null){
//                //没有登录，响应给前端“未登录”状态
//                out(response, ResultCodeEnum.LOGIN_AUTH);
//            }
//        }
//        return chain.filter(exchange);//放行
//    }
//
//    private User getUserInfo(ServerHttpRequest request) {
//        String token = "";
//        List<String> tokenList = request.getHeaders().get("token");
//        if(null  != tokenList) {
//            token = tokenList.get(0);
//        }
//        String userInfoJson = redisTemplate.opsForValue().get("token:" + token);
//        if (StringUtils.isEmpty(userInfoJson)){
//            return null;
//        }
//        //得到redis中的userInfo
//        User userInfo = JSON.parseObject(userInfoJson, User.class);
//        //存储在一个变量里
//        AuthThreadLocalUtils.setThreadLocal(userInfo);
//        return userInfo;
//    }
//
//    private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
//        Result result = Result.build(null, resultCodeEnum);
//        byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
//        DataBuffer buffer = response.bufferFactory().wrap(bits);
//        //指定编码，否则在浏览器中会中文乱码
//        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        return response.writeWith(Mono.just(buffer));
//    }
//}
